package cn.tedu.zhyl.nurseManager.service.impl;

import cn.tedu.zhyl.base.exception.ServiceException;
import cn.tedu.zhyl.base.response.PageResponse;
import cn.tedu.zhyl.base.utils.AuthContext;
import cn.tedu.zhyl.nurseManager.mapper.NursePlanMapper;
import cn.tedu.zhyl.nurseManager.pojo.dto.NursePlanParam;
import cn.tedu.zhyl.nurseManager.pojo.dto.NursePlanQuery;
import cn.tedu.zhyl.nurseManager.pojo.vo.NursePlanVO;
import cn.tedu.zhyl.nurseManager.service.NursePlanService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.*;

@Slf4j
@Service
public class NursePlanServiceImpl implements NursePlanService {
    @Autowired
    private NursePlanMapper nursePlanMapper;
    @Override
    public PageResponse<NursePlanVO> select(NursePlanQuery nursePlanQuery, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        log.debug("查询护理计划业务:nursePlanQuery={}", nursePlanQuery);
        List<NursePlanVO> list = nursePlanMapper.select(nursePlanQuery);
        return PageResponse.of(new PageInfo<>(list));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(NursePlanParam planParam) {
        Integer id = planParam.getId();
        if (ObjectUtils.isEmpty(id)){
            planParam.setStatus(1); // 默认开启
            planParam.setCreateBy((AuthContext.get().getId())); // 设置创建人

            nursePlanMapper.insert(planParam);
            List<Map<String, Object>> jobs = planParam.getItems();
            insertPlanItems(planParam, jobs);
        }else {
            // 更新计划名称
            nursePlanMapper.update(planParam);
            // 先删除所有计划项
            nursePlanMapper.deletePlanItems(planParam);
            // 在添加更新的
            insertPlanItems(planParam,planParam.getItems());
        }
    }

    // 插入护理计划项
    private void insertPlanItems(NursePlanParam planParam, List<Map<String, Object>> jobs) {
        if(jobs == null){
            throw new ServiceException(400,"护理项目不能为空！");
        }
        jobs.stream()
                .filter(Objects::nonNull)
                .forEach(job -> {
//                    if (job.containsKey("planId")) {
//                        throw new ServiceException(400, "护理项目已包含计划ID，请检查数据完整性！");
//                    }
                    job.put("planId", planParam.getId());
                });
        nursePlanMapper.insertPlanJob(jobs);
    }

    @Override
    public List<Map<String, Object>> getItemsByPid(Integer pid) {
        List<Map<String, Object>> list = nursePlanMapper.getItemsByPid(pid);
        return list;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deletePlan(Integer planId) {
        NursePlanParam nursePlanParam = new NursePlanParam();
        nursePlanParam.setId(planId);
        nursePlanMapper.deletePlan(planId);
        nursePlanMapper.deletePlanItems(nursePlanParam);
    }
}
